package com.yhsoft.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yhsoft.common.response.PageResult;
import com.yhsoft.common.vo.Result;
import com.yhsoft.goods.controller.Po.SetSkuInfoPo;
import com.yhsoft.goods.controller.Po.SkuChargeInfoPo;
import com.yhsoft.goods.controller.Po.skuInfoPo;
import com.yhsoft.goods.controller.requset.*;
import com.yhsoft.goods.controller.response.*;
import com.yhsoft.goods.entity.CompanyGoods;
import com.yhsoft.goods.entity.SkuInfo;
import com.yhsoft.goods.service.ICompanyGoodsService;
import com.yhsoft.goods.service.IGoodsInfoService;
import com.yhsoft.goods.service.ISkuInfoService;
import com.yhsoft.sys.entity.CompanyInfo;
import com.yhsoft.sys.service.ICompanyInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品信息表 前端控制器
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-09-22
 */
@Api(tags = {"B-商品接口列表"})
@RestController
@RequestMapping("/goods")
public class GoodsInfoController {
    @Resource
    private IGoodsInfoService goodsInfoService;

    @Resource
    private ISkuInfoService iSkuInfoService;

    @Resource
    private ICompanyGoodsService iCompanyGoodsService;

    @Resource
    private ICompanyInfoService companyInfoService;

    @ApiOperation("商品管理（查询所有可售商品列表）")
    @GetMapping("/getAllGoods")
    public Result<PageResult<AllGoodsOutVo>> getAllGoods(AllGoodsInVo inVo,
                                                         @RequestParam(value = "pageNo") Long pageNo,
                                                         @RequestParam(value = "pageSize") Long pageSize) {
        IPage<AllGoodsOutVo> iPage = goodsInfoService.getAllGoods(inVo, pageNo, pageSize);
        return Result.success(PageResult.valueOf(iPage));
    }
    @ApiOperation("渠道商 商品管理")
    @GetMapping("/getGoodsBySaler")
    public Result<PageResult<CompanyGoodsOutVo>> getGoodsBySaler(AllGoodsInVo inVo,
                                                         @RequestParam(value = "pageNo") Long pageNo,
                                                         @RequestParam(value = "pageSize") Long pageSize) {
        IPage<CompanyGoodsOutVo> iPage = goodsInfoService.getGoodsBySaler(inVo, pageNo, pageSize);
        return Result.success(PageResult.valueOf(iPage));
    }

    @ApiOperation("根据渠道商id分页查询可售商品列表")
    @GetMapping("/getGoodsInfoListByCompanyId")
    public Result<PageResult<GoodsInfoListByCompanyIdOutVo>> getGoodsInfoListByCompanyId(GoodsInfoListByCompanyIdInVo inVo,
                                                                                         @RequestParam(value = "pageNo") Long pageNo,
                                                                                         @RequestParam(value = "pageSize") Long pageSize) {
        IPage<GoodsInfoListByCompanyIdOutVo> iPage = goodsInfoService.getGoodsListByGId(inVo, pageNo, pageSize);
        return Result.success(PageResult.valueOf(iPage));
    }

    @ApiOperation("商品管理（渠道商）")
    @GetMapping("/getDisGoods")
    public Result<PageResult<DisGoodsOutVo>> getDisGoods(DisGoodsInVo inVo,
                                                         @RequestParam(value = "pageNo") Long pageNo,
                                                         @RequestParam(value = "pageSize") Long pageSize) {
        IPage<DisGoodsOutVo> iPage = goodsInfoService.getDisGoods(inVo, pageNo, pageSize);
        return Result.success(PageResult.valueOf(iPage));
    }

    @ApiOperation("按分类删除渠道可售商品")
    @PostMapping("/delDistGoodsByType")
    public Result<?> delDistGoodsByType(@RequestBody DelDistGoodsByTypeInVo inVo) {
        LambdaQueryWrapper<CompanyGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CompanyGoods::getCompanyId, inVo.getDistributorId());
        wrapper.inSql(CompanyGoods::getGoodsId,"select id as goods_id from goods_info where goods_type = " + inVo.getGoodsType());
        iCompanyGoodsService.getBaseMapper().delete(wrapper);
        return Result.success("删除成功！");
    }

    @ApiOperation("按商品ID批量删除渠道可售商品")
    @PostMapping("/delDistGoodsByGoodsId")
    public Result<?> delDistGoodsByGoodsId(@RequestBody DelDistGoodsByGoodsIdInVo inVo) {
        LambdaQueryWrapper<CompanyGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CompanyGoods::getCompanyId, inVo.getDistributorId());
        wrapper.in(CompanyGoods::getGoodsId, inVo.getGoodsIds());
        iCompanyGoodsService.getBaseMapper().delete(wrapper);
        return Result.success("删除成功！");
    }

    @ApiOperation("查询商品服务费")
    @GetMapping("/getSkuChargeInfo")
    public Result<SkuInfoOutVo> getSkuChargeInfo(SkuInfoInVo inVo) {
        List<SkuChargeInfoPo> skuInfoPos = iSkuInfoService.getSkuChargeInfoByGoodsId(inVo.getGoodsId());
        SkuInfoOutVo outVo = goodsInfoService.getSkuInfoOutVo(inVo);
        outVo.setSkuInfoList(skuInfoPos);
        return Result.success(outVo);
    }

    @ApiOperation("设置商品服务费")
    @PostMapping("/setSkuChargeInfo")
    public Result<?> setSkuChargeInfo(@RequestBody SetSkuChargeInfoInVo inVo) {
        LambdaQueryWrapper<CompanyGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CompanyGoods::getCompanyId, inVo.getDistributorId());
        wrapper.eq(CompanyGoods::getGoodsId, inVo.getGoodsId());
        CompanyGoods companyGoods = iCompanyGoodsService.getBaseMapper().selectOne(wrapper);
        companyGoods.setCustomCharge(inVo.getCharge());
        iCompanyGoodsService.updateById(companyGoods);
        return Result.success("保存成功！");
    }

    @ApiOperation("查询商品sku建议售价")
    @GetMapping("/getSkuAdvocatePrice")
    public Result<List<skuInfoPo>> getSkuAdvocatePrice(@RequestParam(value = "goodsId") Integer goodsId) {
        List<skuInfoPo> skuInfos = iSkuInfoService.getSkuInfoByGoodsId(goodsId);
        return Result.success(skuInfos);
    }

    @ApiOperation("设置商品sku建议售价/库存")
    @PostMapping("/setSkuAdvocatePriceOrStock")
    public Result<?> setSkuAdvocatePrice(@RequestBody SetSkuAdvocatePriceInVo inVo) {
        List<SetSkuInfoPo> skuInfos = inVo.getSetSkuInfoPos();
        List<SkuInfo> skuInfos1 = skuInfos.stream().map(r -> {
            if (r.getAdvocatePrice() != null  || r.getStock() != null) {
                SkuInfo skuInfo = new SkuInfo();
                skuInfo.setId(r.getId());
                skuInfo.setAdvocatePrice(r.getAdvocatePrice());
                skuInfo.setStock(r.getStock());
                return skuInfo;
            }
            return null;
        }).collect(Collectors.toList());
        skuInfos1 = skuInfos1.stream().filter(r -> r!=null).collect(Collectors.toList());
        if (skuInfos1.size() > 0) {
            iSkuInfoService.updateBatchById(skuInfos1);
            return Result.success("设置成功！");
        }
        return Result.success("无需设置！");
    }

    @ApiOperation("新增渠道商可售商品")
    @PostMapping("/addDistGoods")
    public Result<?> addDistGoods(@RequestBody AddDistGoodsInVo inVo) {
        if (inVo.getDistributorId() == null || inVo.getGoodsIds().size() == 0) {
            return Result.success("输入非法！");
        }

        Integer distributorId = inVo.getDistributorId();
        // 没输入服务费的时候，设置默认的服务费
        if(!StringUtils.hasLength(inVo.getCharge())){
            CompanyInfo companyInfo = companyInfoService.getById(distributorId);
            if(!StringUtils.hasLength(companyInfo.getDefaultCharge())){
                inVo.setCharge("0");
            }
            inVo.setCharge(companyInfo.getDefaultCharge());
        }

        LambdaQueryWrapper<CompanyGoods> wrapper = new LambdaQueryWrapper<>();
        for(Integer a : inVo.getGoodsIds()){
            CompanyGoods goods = new CompanyGoods();
            goods.setGoodsId(a);
            goods.setCompanyId(distributorId);
            goods.setDeleted(0);
            goods.setCustomCharge(inVo.getCharge());

            wrapper.clear();
            wrapper.eq(CompanyGoods::getCompanyId, distributorId);
            wrapper.eq(CompanyGoods::getGoodsId, a);
            iCompanyGoodsService.saveOrUpdate(goods, wrapper);
        }

        return Result.success("新增成功！");
    }

    @ApiOperation("查询商品详情")
    @GetMapping("/getGoodInfo")
    public Result<GoodsOutVo> getGoodInfo(@RequestParam(value = "goodsId") Integer goodsId) {
        GoodsOutVo outVo = goodsInfoService.getGoodInfoByGoodsId(goodsId);
        return Result.success(outVo);
    }


}
